﻿using NetBay.Core.BusinessEntities.Workflow;

namespace Rio.Manager.Application.Extensions
{
    public static class WorkflowDefinitionExtensions
    {
        #region Methods

        /// <summary>
        /// Clones the specified definition.
        /// </summary>
        /// <param name="definition">The definition.</param>
        /// <returns></returns>
        public static WorkflowDefinition Clone(this WorkflowDefinition definition)
        {
            WorkflowDefinition result = new WorkflowDefinition()
            {
                AssemblyName = definition.AssemblyName,
                ClassName = definition.ClassName,
                Description = definition.Description,
                EndDate = definition.EndDate,
                FolderTypeId = definition.FolderTypeId,
                Identifier = definition.Identifier,
                Name = definition.Name,
                ParentId = definition.ParentId,
                ReceiverGroupId = definition.ReceiverGroupId,
                ReceiverIndexId = definition.ReceiverIndexId,
                ReceiversType = definition.ReceiversType,
                Rules = definition.Rules,
                StartDate = definition.StartDate,
                WorkflowType = definition.WorkflowType,
                ModelId = definition.ModelId,
                IsHierarchical = definition.IsHierarchical
            };

            foreach (var item in definition.Actions)
            {
                result.Actions.Add(item);
            }

            foreach (var item in definition.Conditions)
            {
                result.Conditions.Add(item);
            }

            return result;
        }

        /// <summary>
        /// Converts to model.
        /// </summary>
        /// <param name="definition">The definition.</param>
        /// <returns></returns>
        public static WorkflowModel ConvertToModel(this WorkflowDefinition definition)
        {
            WorkflowModel result = new WorkflowModel()
            {
                AssemblyName = definition.AssemblyName,
                ClassName = definition.ClassName,
                Description = definition.Description,
                Name = definition.Name,
                Rules = definition.Rules,
                WorkflowType = definition.WorkflowType,
            };

            foreach (var item in definition.Actions)
            {
                result.Actions.Add(item);
            }

            foreach (var item in definition.Conditions)
            {
                result.Conditions.Add(item);
            }

            return result;
        }

        #endregion Methods
    }
}